<template>
  <div class="app-container">
    <!--查询-->
    <el-row :gutter="12" style="margin-bottom: 25px">
      <el-card shadow="hover">
        <span>订单号 </span>
        <el-input v-model="orderNo" placeholder="请输入需要查询的订单号" style="width: 200px"></el-input>
        <el-button type="success" style="margin-left: 10px">查询</el-button>
      </el-card>
    </el-row>
    <!--列表-->
    <el-row :gutter="12">
      <el-col :span="24">
        <el-card shadow="hover">
          <el-menu :default-active="0" class="el-menu-demo" mode="horizontal" @select="handleSelect">
            <el-menu-item :index="0">全部</el-menu-item>
            <el-menu-item
                v-for="(state, i) in orderState"
                :key="i+1"
                :index="i+1"
            >
              {{ state }}
            </el-menu-item>
          </el-menu>
          <div class="line"></div>
          <el-table
              style="margin-top: 10px"
              :data="list"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row
          >
            <el-table-column type="selection" width="55"/>
            <el-table-column label="订单号" prop="orderNo"/>
            <el-table-column label="商品信息" prop="skuCode"/>
            <el-table-column label="订单金额" prop="money"/>
            <el-table-column label="买家昵称" width="110" prop="buyerId" align="center"/>
            <el-table-column label="团购编号" width="110" align="center" prop="groupId"/>
            <el-table-column class-name="status-col" label="订单状态" width="110" align="center">
              <template slot-scope="scope">
                <el-tag>{{ orderState[scope.row.state - 1] }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="created_at" label="创建时间" width="200">
              <template slot-scope="scope">
                <i class="el-icon-time"/>
                <span>{{ scope.row.gmtCreate }}</span>
              </template>
            </el-table-column>
            <el-table-column label="查看详情" width="110" align="center">
              <template>
                <el-button type="text" style="color: #07c160">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--    分页   -->
          <div class="block" style="padding-top: 15px;float: right">
            <el-pagination
                :current-page="currentPage"
                :page-sizes="[1,5, 10,20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      orderNo: '',
      pageSize: 5,
      pageCount: 0,
      currentPage: 1, /* 当前页码 */
      total: 0,
      orderState: ['已支付', '已签收', '待退款', '已取消'],
      list: [],
      state:0,
    }
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key)
      this.state=key
      this.currentPage=1
      if(this.state===0){
        this.loadOrderList()
      }else {
        this.loadOrderByState()
      }
    },
    onSubmit() {
      console.log('submit!')
    },
    // 分页的方法
    /* 每页条数改变时，选择一页显示多少行 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.currentPage = 1
      this.pageSize = val
      if(this.state===0){
        this.loadOrderList()
      }else {
        this.loadOrderByState()
      }
    },
    /* 当前页面改变时，跳转其他页 */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      if(this.state===0){
        this.loadOrderList()
      }else {
        this.loadOrderByState()
      }
    },
    loadOrderList(){
      let url='http://localhost:8250/orders/sellerId/'+localStorage.getItem('userid')+'/enable?page='+this.currentPage+'&pageSize='+this.pageSize
      console.log('url = ' + url);
      this.$axios
          .create({'headers': {'Authorization':localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('data=' , responseBody.data);
        this.list=responseBody.data.list;
        this.pageCount=responseBody.data.totalPage
        this.total=responseBody.data.total

      });
    },
    loadOrderByState(){
      let url='http://localhost:8250/orders/sellerId/'+localStorage.getItem('userid')+'/state/'+this.state+'/enable?page='+this.currentPage+'&pageSize='+this.pageSize
      console.log('url = ' + url);
      this.$axios
          .create({'headers': {'Authorization':localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('data=' , responseBody.data);
        this.list=responseBody.data.list;
        this.pageCount=responseBody.data.totalPage
        this.total=responseBody.data.total

      });
    },
  },
  mounted() {
    this.loadOrderList();
  }

}
</script>

<style>
.el-menu--horizontal > .el-menu-item.is-active {
  border-bottom: 2px solid #07c160;
}

.el-button--success {
  background-color: #07c160;
}
</style>
